package com.hooya.fa.eu.biz.dao.manager.special;

import com.hooya.fa.eu.biz.dao.entity.*;
import com.hooya.integrated.common.core.util.BeanCopyUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 每日数据pkpy后的最终处理
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/3/18
 * @Version 1.0.0
 */
@Slf4j
@Component
@AllArgsConstructor
public class FinalDataManager {

    public void dealJcZyThenPkpy(List<TempJcjl> tempJcjlList, List<TempZyjl> tempZyjlList,
                                 List<TempKcjlDaily> turnPkpyRes, List<TempKcjlDaily> inventoryList,
                                 List<TempPkpy> pkpyList) {
        if (!CollectionUtils.isEmpty(tempJcjlList)) {
            List<TempJcjl> jcSzkList = tempJcjlList.stream().filter(l -> l.getPlace().equals("EU生资库"))
                    .collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(jcSzkList)) {
                List<TempPkpy> jcList =
                        BeanCopyUtils.copyListProperties(jcSzkList, TempPkpy::new, (s, t) -> {
                            t.setSl(s.getCgsl());
                            t.setDate(s.getHtrq());
                        });
                List<TempKcjlDaily> jcPkpyList =
                        BeanCopyUtils.copyListProperties(jcSzkList, TempKcjlDaily::new, (s, t) -> {
                            t.setSl(s.getCgsl());
                            t.setDate(s.getHtrq());
                        });
                pkpyList.addAll(jcList);
                turnPkpyRes.addAll(jcPkpyList);
                tempJcjlList.removeAll(jcSzkList);
            }
        }
        if (!CollectionUtils.isEmpty(tempZyjlList)) {
            List<TempZyjl> zySzkList = tempZyjlList.stream().filter(l -> l.getZlck().equals("EU生资库"))
                    .collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(zySzkList)) {
                List<TempPkpy> zyList =
                        BeanCopyUtils.copyListProperties(zySzkList, TempPkpy::new, (s, t) -> {
                            t.setSl(s.getCgsl());
                            t.setPlace(s.getZcck());
                            t.setBelongs(s.getFrombelongs());
                            t.setDate(s.getHtrq());
                        });
                List<TempKcjlDaily> zyPkpyList =
                        BeanCopyUtils.copyListProperties(zySzkList, TempKcjlDaily::new, (s, t) -> {
                            t.setSl(s.getCgsl());
                            t.setPlace(s.getZlck());
                            t.setBelongs(s.getTobelongs());
                            t.setDate(s.getHtrq());
                        });
                pkpyList.addAll(zyList);
                turnPkpyRes.addAll(zyPkpyList);
                tempZyjlList.removeAll(zySzkList);
            }
        }
        pkpy(turnPkpyRes, inventoryList);
    }

    private void pkpy(List<TempKcjlDaily> pkpyList, List<TempKcjlDaily> inventoryList) {
        if (!CollectionUtils.isEmpty(pkpyList)) {
            for (TempKcjlDaily pkpy : pkpyList) {
                inventoryList.stream().filter(l -> l.getCpbh().equals(pkpy.getCpbh()) &&
                                l.getPlace().equals(pkpy.getPlace()) && l.getBelongs().equals(pkpy.getBelongs()))
                        .forEach(l -> l.setSl(l.getSl() - pkpy.getSl()));
            }
        }
    }

    public void dealFinalData(String curDate, List<TempKcjlDaily> inventoryList,
                              List<TempKcjlDaily> allInventoryList, List<TempXsjl> tempXsjlList,
                              List<TradeRecord> tradeRecordList,
                              List<TempXsjlAbnormal> tempXsjlAbnormalList,
                              List<TempXsjlDelete> tempXsjlDeleteList) {
        if (!CollectionUtils.isEmpty(tradeRecordList)) {
            List<TradeRecord> szkTradeList = tradeRecordList.stream().filter(l ->
                            (StringUtils.isNotBlank(l.getZlck()) && l.getZlck().equals("EU生资库")))
                    .collect(Collectors.toList());
            tradeRecordList.removeAll(szkTradeList);
        }
        //去掉数量为0的仓库记录
        List<TempKcjlDaily> removeKcjl = inventoryList.stream().filter(l -> l.getSl() == 0)
                .collect(Collectors.toList());
        inventoryList.removeAll(removeKcjl);
        allInventoryList.addAll(BeanCopyUtils.copyListProperties(inventoryList, TempKcjlDaily::new,
                (s, t) -> t.setDate(curDate)));
        if (!CollectionUtils.isEmpty(tempXsjlAbnormalList)) {
            List<String> sheetNumber = tempXsjlAbnormalList.stream()
                    .map(TempXsjlAbnormal::getSheetNumber).collect(Collectors.toList());
            List<TempXsjl> removeXsList = tempXsjlList.stream().filter(l -> sheetNumber.contains(l.getSheetNumber()))
                    .collect(Collectors.toList());
            tempXsjlList.removeAll(removeXsList);
        }
        if (!CollectionUtils.isEmpty(tempXsjlDeleteList)) {
            List<String> sheetNumber = tempXsjlDeleteList.stream()
                    .map(TempXsjlDelete::getSheetNumber).collect(Collectors.toList());
            List<TempXsjl> removeXsList = tempXsjlList.stream().filter(l -> sheetNumber.contains(l.getSheetNumber()))
                    .collect(Collectors.toList());
            tempXsjlList.removeAll(removeXsList);
        }
    }

}
